package com.abigdreamer.swing.app;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;

import javax.imageio.ImageIO;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.UIDefaults;
import javax.swing.UIManager;

import com.abigdreamer.swing.app.skin.SkinConst;
import com.abigdreamer.swing.app.skin.SkinManager;
import com.abigdreamer.swing.component.UIUtil;
import com.abigdreamer.util.ImageUtil;
import com.abigdreamer.util.SwingResourceManager;

import craky.util.Util;

public class SwingUtil
{
  public static Dimension stringToSize(String sizeDes)
  {
    String[] weights = sizeDes.split(",");
    Dimension size = new Dimension();
    if (weights.length == 2) {
      try
      {
        size.setSize(Integer.parseInt(weights[0]), Integer.parseInt(weights[1]));
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
    return size;
  }
  
  public static String sizeToString(Dimension size)
  {
    return size.width + "," + size.height;
  }
  
  public static Image getImage(String name)
  {
    return SwingResourceManager.getImage(SkinConst.IMAGE_DIR + SkinConst.FILE_SEP + name);
  }
  
  public static BufferedImage getBufferedImage(String name)
  {
    try
    {
      return ImageIO.read(new File(SkinConst.IMAGE_DIR + SkinConst.FILE_SEP + name));
    }
    catch (MalformedURLException e)
    {
      e.printStackTrace();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    return null;
  }
  
  public static Image getImage(String name, boolean createdByIcon)
  {
    if (createdByIcon) {
      return new ImageIcon(SkinConst.IMAGE_DIR + SkinConst.FILE_SEP + name).getImage();
    }
    return getImage(name);
  }
  
  public static Icon getIcon(String name, int thumbWidth, int thumbHeight)
  {
    try
    {
      return new ImageIcon(ImageUtil.createThumbnail(getImage(name), thumbWidth, thumbHeight, 100));
    }
    catch (FileNotFoundException e)
    {
      e.printStackTrace();
    }
    catch (InterruptedException e)
    {
      e.printStackTrace();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    return null;
  }
  
  public static Icon getIcon(String name)
  {
    return SwingResourceManager.getIcon(SkinConst.IMAGE_DIR + SkinConst.FILE_SEP + name);
  }
  
  public static Icon getIcon(String name, boolean createdByIcon)
  {
    if (createdByIcon) {
      return new ImageIcon(SkinConst.IMAGE_DIR + SkinConst.FILE_SEP + name);
    }
    return getIcon(name);
  }
  
  public static void switchToSystemLAFTemp(Action action)
  {
    if (!Util.isWindows())
    {
      action.actionPerformed(null);
      return;
    }
    try
    {
      String sysLafclassName = UIManager.getSystemLookAndFeelClassName();
      String currentLafClassName = UIManager.getLookAndFeel().getClass().getName();
      if (!sysLafclassName.equals(currentLafClassName))
      {
        UIDefaults uid = UIManager.getLookAndFeelDefaults();
        HashMap<Object, Object> oldDefaultsMap = new HashMap();
        Class<?> clazz = Class.forName(sysLafclassName);
        LookAndFeel sysLaf = (LookAndFeel)clazz.newInstance();
        oldDefaultsMap.putAll(uid);
        uid.clear();
        uid.putAll(sysLaf.getDefaults());
        action.actionPerformed(null);
        uid.clear();
        uid.putAll(oldDefaultsMap);
        oldDefaultsMap.clear();
        oldDefaultsMap = null;
      }
      else
      {
        action.actionPerformed(null);
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
  
  public static void runApp(Class<? extends SkinableWindow> appClass)
  {
    try
    {
      runApp((SkinableWindow)appClass.newInstance());
    }
    catch (InstantiationException e)
    {
      e.printStackTrace();
    }
    catch (IllegalAccessException e)
    {
      e.printStackTrace();
    }
  }
  
  public static void runApp(final SkinableWindow app)
  {
    System.setProperty("sun.java2d.noddraw", "true");
    ToolTipManager.sharedInstance().setInitialDelay(200);
    UIUtil.setPopupMenuConsumeEventOnClose(false);
    if (Util.isWindows()) {
      UIUtil.initToolTipForSystemStyle();
    }
    UIUtil.hideInputRect();
    
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        SkinManager.loadAllSkin();
        
        app.showWindow();
        
        SkinManager.initSkin();
      }
    });
  }
}
